import traceback
import concurrent.futures
from util.data_utils import *
import sys,os
from urllib.parse import urlparse, parse_qs, urlencode
import time
import myglobal
from concurrent.futures import ProcessPoolExecutor
import math
#from aa import quick_generate_cookie

from datetime import datetime


#博主
def process_user_url(user_id, page=1):
    """主函数"""
    # 从文件加载cookies
    cookies_dict = load_cookies_from_file("weibo_cookies.json")
    
    if not cookies_dict:
        print("无法加载cookies，程序退出")
        return
    # 创建请求头
    headers = create_weibo_headers(cookies_dict)
    # 获取第一页数据测试
    print("=== 测试获取第一页数据 ===")
    data = get_user_weibo_list(user_id, headers, page)
    success_count=0

    if data:
        all_weibos = get_multiple_pages_weibo(user_id, headers, 1, page)
        total_count = len(all_weibos)
        print(f"总共获取到 {len(all_weibos)} 条微博")
        for result in all_weibos:
            sleep_time = random.uniform(5, 10)
            time.sleep(sleep_time)
            print("准备推送单条微博数据:")
            print(result)
            print("==========================")
            
            success = toDatabase(result)
            if success:
                success_count += 1
                print(f"✅ 第 {success_count}/{total_count} 条微博推送成功")
            else:
                print(f"❌ 微博推送失败: {result.get('article_id', '未知ID')}")
                

    
def main():
    print(" --- 【一轮开始】 --- ")
    # 记录开始时间
    start_time = time.time()

    with open(URLS_PATH, mode='r',encoding="utf-8") as f:
        weibo_user_ids = f.read().splitlines()
    # 随机打乱用户ID顺序
    random.shuffle(weibo_user_ids)
    #读取url
    for user_id in weibo_user_ids:
        #print(url)
        process_user_url(user_id)
        time.sleep(random.uniform(5, 10))


    

  

    # 记录结束时间            
    end_time = time.time()
    print(f"✅ 所有任务完成，耗时 {end_time - start_time:.2f} 秒")

    print(" --- 【一轮结束】 --- ")

if __name__ == "__main__":
    """
    https://weibo.com/u/5748988380，小屁屁找
    https://weibo.com/u/2794284831，好心天天分享
    https://weibo.com/u/6439151235，败家少女的日常
    https://weibo.com/u/5371906414，佩奇线报
    微博超话 https://m.weibo.cn/p/index?extparam=U%E5%85%88%E7%B4%A0%E8%B4%A8%E8%AF%95%E7%94%A8&containerid=100808c9f05766724555f036b3383f5cb97e33&luicode=20000061&lfid=5183171777004794
    """
    while True:
        #cookie, filename = quick_generate_cookie()
        #print(f"快速生成Cookie: {filename}")
        main()
        time.sleep(random.uniform(10, 20))
    # https://m.weibo.cn/detail/5221758682793485
    # 5231436089262703
    # result = test_single_weibo_url("https://m.weibo.cn/detail/5221758682793485", push_to_db=False)
    # if result:
    #     print(json.dumps(result, ensure_ascii=False, indent=2))
